package com.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 *  ServletContextListener 监控servlet 上下文的时间，容器初始化时间，容器销毁事件。可做以下系统初始化的工作
 *  ServletContextAttributeListener 监听对ServletContext属性的操作，比如增加、删除、修改
 *  HttpSessionListener : 监听session会话事件，创建和销毁
 *
 *
 */
public class MyHttpSessionListener implements ServletContextListener,HttpSessionListener, ServletContextAttributeListener {

    public int count=0;//记录session的数量
    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        count++;
        arg0.getSession().getServletContext().setAttribute("Count", count);
        MySessionContext.AddSession(arg0.getSession());
        System.out.println(this.toString());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        count--;
        arg0.getSession().getServletContext().setAttribute("Count", count);
        MySessionContext.DelSession(arg0.getSession());
        System.out.println(this.toString());
    }

    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {

    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {

    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {

    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
